//
//  CouponListCell.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/5.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "CouponListCell.h"

@interface CouponListCell ()

@property (nonatomic, strong) UILabel     *moneyLabel;
@property (nonatomic, strong) UIImageView *statuImage;
@property (nonatomic, strong) UILabel     *noteLabel;

@end

@implementation CouponListCell

-(void)setCouponModel:(CouponModel *)couponModel
{
    _couponModel = couponModel;
    if ([couponModel.type integerValue] != 2) {
        NSInteger money = [couponModel.money integerValue] - [couponModel.use_money integerValue];
        NSString *moneyStr = [NSString stringWithFormat:@"%ld",(long)money];
        NSString *string = [NSString stringWithFormat:@"%ld元  优惠券",(long)money];
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:string];
        [attributedStr addAttribute:NSFontAttributeName value:Kfont(18) range:NSMakeRange(moneyStr.length, string.length - moneyStr.length)];
        self.moneyLabel.attributedText = attributedStr;
    }else {
        NSString *string = [NSString stringWithFormat:@"%@折  优惠券",couponModel.money];
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:string];
        [attributedStr addAttribute:NSFontAttributeName value:Kfont(18) range:NSMakeRange(couponModel.money.length, string.length - couponModel.money.length)];
        self.moneyLabel.attributedText = attributedStr;
    }
    self.noteLabel.text = @"";
    if (couponModel.goods_id.length > 0) {
        self.noteLabel.text = [NSString stringWithFormat:@"此券仅限购买%@产品",couponModel.goods_id];
    }
    if (couponModel.condition) {
        if (self.noteLabel.text.length > 0) {
            self.noteLabel.text = [self.noteLabel.text stringByAppendingString:[NSString stringWithFormat:@",此券满%@元使用",couponModel.condition]];
        }else {
            self.noteLabel.text = [NSString stringWithFormat:@"此券满%@元使用",couponModel.condition];
        }
    }
    if (couponModel.num) {
        if (self.noteLabel.text.length > 0) {
            self.noteLabel.text = [self.noteLabel.text stringByAppendingString:[NSString stringWithFormat:@",每个ID限领%@张",couponModel.num]];
        }else {
            self.noteLabel.text = [NSString stringWithFormat:@"每个ID限领%@张",couponModel.num];
        }
    }
    
    if ([couponModel.isUsed integerValue] == 0) {
        self.statuImage.hidden = YES;
    }else {
        self.statuImage.hidden = NO;
    }
}

+ (CouponListCell *)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"CouponListCell";
    CouponListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[CouponListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        [cell createSubView];
    }
    
    return cell;
}

- (void)createSubView {
    UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(15), SCREEN_WIDTH - ScreenX375(26), ScreenX375(89))];
    bgView.image = [UIImage imageNamed:@"优惠券Bg"];
    [self addSubview:bgView];
    
    self.moneyLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(134), ScreenX375(25), SCREEN_WIDTH - ScreenX375(170), ScreenX375(45))];
    self.moneyLabel.textColor = ThemeColor;
    self.moneyLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:40 * SCREEN_WIDTH / 375];
    [self addSubview:self.moneyLabel];
    
    self.noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(73), SCREEN_WIDTH - ScreenX375(26), ScreenX375(28))];
    self.noteLabel.textColor = [UIColor whiteColor];
    self.noteLabel.textAlignment = NSTextAlignmentCenter;
    self.noteLabel.font = Kfont(9);
    [self addSubview:self.noteLabel];
    
    self.statuImage = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(270), ScreenX375(33), ScreenX375(74), ScreenX375(55))];
    self.statuImage.image = [UIImage imageNamed:@"已使用"];
    [self addSubview:self.statuImage];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
